home *** CD-ROM | disk | FTP | other *** search
- Path: unisql.unisql.com!news
- From: Ed Hill <edhill@unisql.com>
- Newsgroups: comp.lang.c++
- Subject: casting w/ virtual base classes
- Date: Mon, 15 Apr 1996 12:17:13 -0500
- Organization: UniSQL, Inc., Austin, Texas, USA
- Message-ID: <31728499.6201DD56@unisql.com>
- NNTP-Posting-Host: mazda.unisql.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.01 (X11; I; SunOS 4.1.3_U1 sun4c)
-
- I have the following class hierarchy
-
- class Derived : public virtual Base {...};
-
- Suppose there is a function foo that returns a pointer to a virtual
- base class.
-
- Base* foo(void) {...}
-
- Now, in my program I have a variable declared as a pointer to Derived.
-
- int main() {
- Derived *dp;
- ...
- exit 0;
- }
-
- Book Explanation:
- =================
- In a virtual derivation, the derived class object contains the derived
- part and a pointer to the virtual base part. The virtual base class is
- not contained within the derived class object.
- ==================
-
- My compiler does not allow either of the two following statements:
-
- Derived *dp1 = foo();
- Derived *dp2 = (Derived *) foo();
-
- error: cast: Base* ->derived dp2*; Base is virtual base
-
- Ok, fine...
-
- However, the following compiles:
-
- Derived *dp = (Derived *) (void *) foo();
-
- Then, I'm able to access members of Derived and get correct data,
- remember foo() returns a pointer to a virtual base.
-
- dp->num; // for example
-
- ===
- Q1: Is the double cast legal? Why? / Why not?
- Q2: How is it possible when from the book explanation, virtual base
- is in a different address space and pointed to from the derived
- class object.
- --
- <><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><><>
-
- Ed Hill
- Technical Services Consultant /\\\
- Object-Relational Database Technologies \/// UniSQL, Inc.
- -----------------------------------------------------------------
- EMAIL: edhill@unisql.com
- WWW: http://www.unisql.com
-